//
//  OutputVC.m
//  Kelindo
//
//  Created by Gianluca Puglia on 22/04/13.
//  Copyright (c) 2013 Gianluca Puglia. All rights reserved.
//

#import "OutputVC.h"

@interface OutputVC ()

@end

@implementation OutputVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //Initialization
    }
    return self;
}

-(void)loadView {
    [super loadView];
    tableArray = [_rd filterOnlyMatch];
    [_destinationCombobox setDelegate:self];
    NSString *deskPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"];
    [_destinationCombobox addItemWithObjectValue:deskPath];
    [_destinationCombobox selectItemAtIndex:0];
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    
    static NSString *cellIdentifier = @"cellViewCheck";
    NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

    NSDictionary *d = [tableArray objectAtIndex:row];
    if( [tableColumn.identifier isEqualToString:@"Title"] ) {    
        cellView.textField.stringValue = [d objectForKey:@"title"];
        return cellView;
    }
    if( [tableColumn.identifier isEqualToString:@"Author"] ) {
        cellView.textField.stringValue = [d objectForKey:@"author"];
        return cellView;
    }
    if( [tableColumn.identifier isEqualToString:@"Directory"] ) {
        cellView.textField.stringValue = [d objectForKey:@"path"];
        return cellView;
    }
    if( [tableColumn.identifier isEqualToString:@"Finalize"] ) {
        NSTableCellView *cellViewCheck = [tableView makeViewWithIdentifier:cellIdentifier owner:self];
        
        if (cellViewCheck == nil) {
            cellViewCheck = [[NSTableCellView alloc] init];
            [cellViewCheck setIdentifier:cellIdentifier];
            NSButton *fin = [[NSButton alloc] initWithFrame:NSMakeRect(20, -1, 20, 20)];
            [fin setTag:1];
            [fin setButtonType:NSSwitchButton];
            [fin setTarget:self];
            [fin setAction:@selector(finalizeChanged:)];
            [cellViewCheck addSubview:fin];
        }
        
        NSButton *fin = [cellViewCheck viewWithTag:1];
        if ([(NSNumber *)[d objectForKey:@"finalize"] isEqual:[NSNumber numberWithInt:1]]) {
            [fin setState:NSOnState];
        }
        else {
            [fin setState:NSOffState];
        }
        if (![[d objectForKey:@"status"] isEqualToString:@"MATCH"]) {
            [fin setEnabled:NO];
        }
        else {
            [fin setEnabled:YES];
        }
        return cellViewCheck;
    }
    return cellView;
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
    NSText *fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
    NSControl *postingObject = [aNotification object];
    if ([postingObject isEqualTo:_destinationCombobox]) {
        [_destinationCombobox insertItemWithObjectValue:fieldEditor.string atIndex:0];
        [_destinationCombobox selectItemWithObjectValue:fieldEditor.string];
    }
}

- (IBAction)cancel:(id)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"close" object:nil];
}

- (IBAction)done:(id)sender {
    [[BridgeClass sharedInstance] finalizeWithAlbums:tableArray
                                              onCopy:[_moveOrCopyCombobox titleOfSelectedItem]
                                              format:[_formatCombobox titleOfSelectedItem]
                                         destination:[_destinationCombobox objectValueOfSelectedItem]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"close" object:nil];
}

- (IBAction)showAll:(id)sender {
    if ([[_showCombobox titleOfSelectedItem] isEqualToString:@"Show all"]) {
        tableArray = [_rd filterAllOrdered];
    }
    else {
        tableArray = [_rd filterOnlyMatch];
    }
    [_tableView reloadData];
}

- (void)finalizeChanged:(id)sender {
    NSButton *b = (NSButton *)sender;
    NSTableCellView *tvc = (NSTableCellView *)b.superview;
    if (b.state == NSOnState) {
        NSDictionary *d = [tableArray objectAtIndex:[_tableView rowForView:tvc]];
        [d setValue:[NSNumber numberWithInt:1] forKey:@"finalize"];
    }
    else {
        NSDictionary *d = [tableArray objectAtIndex:[_tableView rowForView:tvc]];
        [d setValue:[NSNumber numberWithInt:0] forKey:@"finalize"];
    }
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return [tableArray count];
}

@end
